package com.jn.jetpack.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jn.jetpack.R;
import com.jn.jetpack.entity.QAndAEntity;

import java.util.List;

public class QAndAAdapter extends RecyclerView.Adapter<QAndAAdapter.ViewHolder> {
    private List<QAndAEntity> mList;

    public QAndAAdapter(List<QAndAEntity> list) {
        mList = list;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void updateData(List<QAndAEntity> list) {
        mList.clear();
        mList = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.qanda_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        QAndAEntity entity = mList.get(position);
        holder.number.setText(entity.getId() + "");
        holder.question.setText(entity.getQuestion());
        holder.answer.setText(entity.getAnswer());
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView number;
        TextView question;
        TextView answer;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            number = itemView.findViewById(R.id.number);
            question = itemView.findViewById(R.id.question);
            answer = itemView.findViewById(R.id.answer);
        }
    }
}